<?php
/**
 * @author Rick van Schalkwijk <rickvschalkwijk@gmail.com>, Niek Saarberg <n.saarberg@gmail.com>
 * @version 0.0.3
 * @date Februari 28, 2010
 *
 * Todo:
 * - Descripe functions
 *
 * Changes:
 * 0.0.4
 * - Niek S. 
 * 0.0.3
 * - Niek S. Added $filename param to ::write()
 * 0.0.2
 * - Rick S. Made class a singleton pattern
 * 0.0.1
 * - Rick S Class created and added created base functionality
 */

class log {
	
	// object instance
    private static $instance;
 
    private function __construct() {}
    private function __clone() {}
	
    /**
	 *
	 */
	static function write ( $message, $classObject, $function, $filename = '' ) {
		
		if( is_object($classObject) ) {
			
			$className = get_class($classObject);
		} elseif( is_string($classObject) ) {
			
			$className = $classObject;
		}
		
		$filename = ( $filename ) ? '-' . $filename : $filename;
		
		$file = SITE_PATH . 'logs' . DIRSEP . date('d-m-y') . $filename . '.txt';
		$log = date('H:i:s') . ' ' . $className . '::' . $function . ', ' . $message . PHP_EOL;
		
		file_put_contents( $file, $log, FILE_APPEND );
	}
	
	/**
	 *
	 */
	static function fatal ( $message, $classObject, $function ) {
		
		if( is_object($classObject) ) {
			
			$className = get_class($classObject);
		} elseif( is_string($classObject) ) {
			
			$className = $classObject;
		}
		
		$file = SITE_PATH . 'logs' . DIRSEP . date('d-m-y') . '-fatal.txt';
		$log = date('H:i:s') . ' ' . $className . '::' . $function . ', ' . $message . PHP_EOL;
		
		file_put_contents( $file, $log, FILE_APPEND );
		
		echo $message;
		
		exit();
	}
	
	/**
	 * PHP custom error handler
	 */
	static function error_handler ( $error_level, $error_message, $error_file, $error_line, $error_context ) {
		
		log::write( $error_message . ' on line ' . $error_line, 'debug_info', $error_file );
		
		return true;
	}
	
	
	/*
	 * Function to log login time and date
	 */
	static function logLogin(){
		
		$file = SITE_PATH . 'logs' . DIRSEP . 'LoginDateTime' . DIRSEP .  date('d-m-y') . $filename . '.txt';
		$log = date('H:i:s') . ' ' . $className . '::' . $function . ', ' . $message . PHP_EOL;
	
	}
}
